package org.wiztools.thegreatwall;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;


/**
 *
 * @author subwiz
 */
public final class DBUtil {

    private static final Logger LOG = Logger.getLogger(DBUtil.class.getName());

    private static DataSource ds;
    private DBUtil(){}

    public static DataSource getDataSource(){
        if(ds == null){
            try{
                ds = (DataSource)new InitialContext().lookup("java:/comp/env/jdbc/TheGreatWallPool");
            }
            catch(NamingException ex){
                new RuntimeException(ex);
            }
        }
        return ds;
    }

    public static void close(ResultSet rs, Statement s, Connection con){
        // Resultset:
        if(rs != null){
            try{
                rs.close();
            }
            catch(SQLException ex){
                LOG.log(Level.SEVERE, null, ex);
            }
        }

        // Statement:
        if(s != null){
            try{
                s.close();
            }
            catch(SQLException ex){
                LOG.log(Level.SEVERE, null, ex);
            }
        }

        // Connection:
        if(con != null){
            try{
                con.close();
            }
            catch(SQLException ex){
                LOG.log(Level.SEVERE, null, ex);
            }
        }
    }
}
